#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"

int testMineEffect(struct gameState *state) {
	/**********************************************\
	| Note that this has uncovered an actual bug in mine's effect, 
	| the > the wrong direction on the getCost check!
	\**********************************************/
	int handCnt, actionCount, i, drawSuccess = 0;
	//put cards in hand
	state->hand[0][1] = mine;
	state->hand[0][2] = gold;
	state->hand[0][3] = village; // cost=3
	state->hand[0][4] = province; // cost=8
	state->hand[0][5] = adventurer; // cost=6
	state->hand[0][6] = copper;
	state->handCount[0] = 7;
	handCnt = state->handCount[0];
	actionCount = state->numActions;
	//play mine illegally (can't discard adventurer)
	assert(-1 == cardEffect(mine, 5, gold, 0, state, 1, 0));
	//play mine illegally again (can't discard province)
	assert(-1 == cardEffect(mine, 4, 5, 0, state, 1, 0));
	//play mine illegally again (requested card is too expensive)
	/********************
	| assert(-1 == cardEffect(mine, 6, gold, 0, state, 1, 0));
	*********************/
	if ( -1 != cardEffect(mine, 6, gold, 0, state, 1, 0)) {
		printf("**************\n\tERRROR:  cardEffect should deny the request to trade in copper for gold, but it allows it\n**************\n");
		
		//put cards back in hand
		state->hand[0][1] = mine;
		state->hand[0][2] = gold;
		state->hand[0][3] = village; // cost=3
		state->hand[0][4] = province; // cost=8
		state->hand[0][5] = adventurer; // cost=6
		state->hand[0][6] = copper;
		state->handCount[0] = 7;
		handCnt = state->handCount[0];
		actionCount = state->numActions;
	}
	//play mine illegally again (requested card does not exit)
	assert(-1 == cardEffect(mine, 6, -1, 0, state, 1, 0));
	//play mine illegally again (requested card does not exist)
	assert(-1 == cardEffect(mine, 6, treasure_map + 1, 0, state, 1, 0));
	printf("\tDenies actions correctly");
	
	//play mine correctly
	//printf("\nreturns: %d\n", cardEffect(mine, 6, silver, 0, state, 1, 0));
	assert(0 == cardEffect(mine, 6, silver, 0, state, 1, 0));
	
	//check that mine is no longer in that position
	assert(mine != state->hand[0][1]);
	printf(", card was discarded");
	//handCount should be one less than before (spent mine, trashed copper, added silver)
	assert(state->handCount[0] == handCnt - 1);
	printf(", handCount is correct");
	//check that silver exists
	//printf("\n\nhandCnt: %d, Check Card Values:\n", state->handCount[0]);
	for (i=0;i<state->handCount[0];i++) {
		if(state->hand[0][i]==silver){
			drawSuccess = 1;
			break;
		}
	}
	assert(drawSuccess = 1);
	printf(", new treasure added!\n");
	return 0;
}

int main() {
    
    int seed = 1000;
    int numPlayer = 3;
    
	struct gameState G;
	struct gameState *state = &G;
	int k[10] = {council_room, smithy, steward, remodel, village, minion, mine, cutpurse, 
		   sea_hag, tribute};

	initializeGame(numPlayer, k, seed, &G);
	printf("Testing mineEffect():\n");
	testMineEffect(state);
	
	printf("   mineEffect() Passed all tests!\n");
	

    return 0;
}

